package org.jeecg.modules.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.cloudauth.model.v20190307.DescribeVerifyResultRequest;
import com.aliyuncs.cloudauth.model.v20190307.DescribeVerifyResultResponse;
import com.aliyuncs.cloudauth.model.v20190307.DescribeVerifyTokenRequest;
import com.aliyuncs.cloudauth.model.v20190307.DescribeVerifyTokenResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import me.zhyd.oauth.utils.StringUtils;
import me.zhyd.oauth.utils.UuidUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class AliAuthUtils {

    public static Map AliAuthen() {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",    // 固定cn-hangzhou
                "LTAI5t6h7VKjjuNWbHqsWzQ3",      // 您的Access Key ID
                "CEBHv7jaDqN0gnjs4oIfmRsCta0io4");  // 您的Access Key Secret
        IAcsClient client = new DefaultAcsClient(profile);

//1. 接入方服务端发起认证请求，获得认证token，接口文档：https://help.aliyun.com/document_detail/154605.html
        DescribeVerifyTokenRequest request = new DescribeVerifyTokenRequest();

        String bizType = "a1a86434555c45eb8caaee9bdcd28a70";
        String bizId = UuidUtils.getUUID();
//具体传参
        request.setRegionId("cn-hangzhou");
        request.setSysProtocol(ProtocolType.HTTPS);
        request.setBizId(bizId);
        request.setBizType(bizType); //创建方法请参见https://help.aliyun.com/document_detail/127885.html
        DescribeVerifyTokenResponse response = null;
        try {
            response = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        String verifyToken = "";
        if (response != null && StringUtils.isNotEmpty(response.getVerifyToken())) {
            verifyToken = response.getVerifyToken();
        }

//2. 接入方服务端将token传递给接入方无线客户端
//3. 接入方无线客户端用token调起无线认证SDK
//4. 用户按照由无线认证SDK组织的认证流程页面的指引，提交认证资料
//5. 认证流程结束退出无线认证SDK，进入客户端回调函数
//6. 接入方服务端获取认证状态和认证资料（注：客户端无线认证SDK回调中也会携带认证状态, 但建议以服务端调接口获取的为准进行业务上的判断和处理）
//查询认证结果接口文档：https://help.aliyun.com/document_detail/154606.html
        Map<String, Object> map = new HashMap();
        map.put("token", verifyToken);
        map.put("RequestId", bizId);
//常见问题：https://help.aliyun.com/document_detail/127993.html
        return map;
    }


    public static DescribeVerifyResultResponse getAliResult(String RequestId, HttpServletRequest request) {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",    // 固定cn-hangzhou
                "LTAI5t6h7VKjjuNWbHqsWzQ3",      // 您的Access Key ID
                "CEBHv7jaDqN0gnjs4oIfmRsCta0io4");  // 您的Access Key Secret
        IAcsClient client = new DefaultAcsClient(profile);
        DescribeVerifyResultRequest verifyResultRequest = new DescribeVerifyResultRequest();
//具体传参
        verifyResultRequest.setRegionId("cn-hangzhou");
        verifyResultRequest.setSysProtocol(ProtocolType.HTTPS);
        verifyResultRequest.setBizId(RequestId);
        verifyResultRequest.setBizType("a1a86434555c45eb8caaee9bdcd28a70");
        DescribeVerifyResultResponse verifyResultResponse = null;
        try {
            verifyResultResponse = client.getAcsResponse(verifyResultRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return verifyResultResponse;
    }
}
